using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using ImageUploaderComponent.Extensions;

namespace ImageUploaderComponent.Data {
    public class ImageDataViewModel : MediaviewModelBase {
        

        /// <summary>
        /// This constructor is called when uploading an new file.  It sets the proper defaults for a brand new Image
        /// </summary>
        /// <param name="file"></param>
        public ImageDataViewModel(FileInfo file) {
            imageData = new ImageData(file);
            StatusImageUri = new Uri("complete.png", UriKind.Relative);
            Name = file.Name.Split('.')[0];
            ImageBytes = file.OpenRead().ToArray();
            IsDeletable = false;
            progress = 0;
        }

        /// <summary>
        /// This is only called when loading from data
        /// </summary>
        /// <param name="data"></param>
        public ImageDataViewModel(ImageData data){
            imageData = data;
            isDeletable = false;
            isUploading = false;
            StatusImageUri = new Uri("complete.png", UriKind.Relative);
        }


        public string LargeFileName {
            get { return ((ImageData)imageData).LargeFileName; }
            set {
                if (value != ((ImageData)imageData).LargeFileName) {
                    propertyChanged("LargeFileName");
                }
                ((ImageData)imageData).LargeFileName = value;
            }
        }



        public override UploadStatus validateMedia(){
            if (!isLegalFileExtension(FileName)) {
                return UploadStatus.InvalidExtension;
            }
            return UploadStatus.ValidUploadAttempt;
        }

        protected override bool isLegalFileExtension(string fileName) {
            string extension = Path.GetExtension(fileName);
            var legalFileNames = new List<string> { ".jpg", ".png" };
            if (legalFileNames.Contains(extension.ToLower())) {
                return true;
            }
            return false;
        }

    }
}